package com.cafe.action.admin;

import org.hibernate.criterion.DetachedCriteria;
import org.springframework.beans.factory.annotation.Autowired;

import com.cafe.model.Notice;
import com.cafe.service.NoticeService;
import com.malamute.core.action.BaseAction;
import com.malamute.core.action.CrudMethod;
import com.malamute.core.covert.DateTypeConverter;

/**
 * @author Aceguan
 * @description TODO
 * @date 2012-2-26 下午10:15:32
 * @version v1.0
 */
public class NoticeAction extends BaseAction implements CrudMethod {
	@Autowired
	private NoticeService noticeService;

	private Notice notice;

	public Notice getNotice() {
		return notice;
	}

	public void setNotice(Notice notice) {
		this.notice = notice;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @return
	 * 
	 * @throws Exception
	 * 
	 * @see com.malamute.core.action.CrudMethod#save()
	 */
	@Override
	public String save() throws Exception {
		noticeService.saveNotice(notice);
		return ACTION;
	}

	@Override
	public String form() throws Exception {
		if (notice != null) {
			setDateType(DateTypeConverter.DATE_TYPES[1]);
			setNotice(noticeService.getNoticeById(notice.getId()));
		}
		return FORM;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @return
	 * 
	 * @throws Exception
	 * 
	 * @see com.malamute.core.action.CrudMethod#update()
	 */
	@Override
	public String update() throws Exception {
		noticeService.updateNotice(notice);
		return ACTION;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @return
	 * 
	 * @throws Exception
	 * 
	 * @see com.malamute.core.action.CrudMethod#list()
	 */
	@Override
	public String list() throws Exception {
		DetachedCriteria criteria = DetachedCriteria.forClass(Notice.class);

		noticeService.listNoticesByPage(criteria, page);
		return LIST;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @return
	 * 
	 * @throws Exception
	 * 
	 * @see com.malamute.core.action.CrudMethod#search()
	 */
	@Override
	public String search() throws Exception {
		return this.list();
	}
}
